package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.agree.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import org.junit.Assert;

import java.util.Arrays;
import java.util.List;

public class PassAnalyzeWorkReportApprovalLevelFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
        workFlowTaskQuery.instanceStatusList.add(WorkFlowInstanceStatus.Running.status);
        workFlowTaskQuery.complete = false;

        List<String> assignerList = Arrays.asList("李四", "王五");
        for(int i=0;i<assignerList.size();i++){
            workFlowTaskQuery.assignerList = Arrays.asList(assignerList.get(i));
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            boolean instanceFinished = quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            if(i<assignerList.size()-1){
                Assert.assertFalse("实例当前并未结束", instanceFinished);
            }
        }
    }

    @Override
    public String name() {
        return "分析工作报告-逐级审批-全部同意";
    }

}
